package top.suven.base.http.admin.sec.controller;

import org.apache.commons.lang3.StringUtils;
import top.suven.base.core.mybatis.PageData;
import top.suven.base.http.admin.frame.config.ResponseResult;
import top.suven.base.http.admin.sec.model.Role;
import top.suven.base.http.admin.sec.service.RoleService;
import top.suven.base.http.admin.sec.vo.request.RoleRequestVo;
import top.suven.base.http.admin.sec.vo.response.RoleResponseVo;
import top.suven.base.http.admin.sec.vo.valid.SecValidGroup;
import top.suven.base.http.enums.EnableStatusEnum;
import top.suven.base.http.enums.StatusEnum;
import top.suven.base.http.enums.SysMsgEnumType;
import top.suven.base.util.utilize.BeanUtil;
import top.suven.base.util.utilize.DateUtil;
import top.suven.base.common.excel.ColumnBox;
import top.suven.base.common.excel.ExportUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**   
 * @Title: RoleController.java
 * @Description: 角色的控制服务类
 * @author lixiangling
 * @date   2018/08/13 16:58:08
 * @version V1.0
 * ----------------------------------------------------------------------------
 *  modifyer    modifyTime                 comment
 *
 * ----------------------------------------------------------------------------
 *
 */
@Controller
@RequestMapping("/sec/role")
public class RoleController {
    private final static Logger logger = LoggerFactory.getLogger(RoleController.class);
	@Autowired
	private RoleService roleService;
    /**
     * @Title: 跳转到角色主界面
     * @Description:roleRequestVo @{Link RoleRequestVo}
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 16:58:08
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "")
    public String index(){
        return "sec/role_index";
    }
    /**
     * @Title: 获取角色分页信息
     * @Description:roleRequestVo @{Link RoleRequestVo}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 16:58:08
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping(value = "list")
    public PageData<RoleResponseVo> list(@Validated(value = {SecValidGroup.SecQueryGroup.class}) RoleRequestVo roleRequestVo){
        Role role = new Role();
        BeanUtil.copyProperties(roleRequestVo, role);
        return roleService.getRolePage(role, roleRequestVo.getPage(), roleRequestVo.getLimit());
    }
   /**
    * @Title: 保存角色信息
    * @Description:roleRequestVo @{Link RoleRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 16:58:08
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "save")
    public ResponseResult save(@Validated(value = {SecValidGroup.SecFormGroup.class}) RoleRequestVo roleRequestVo){
        Role role = new Role();
        BeanUtil.copyProperties(roleRequestVo, role);
        if(role.getRoleCode() != null){
            roleService.updRole(role);
        }else{
            role.setStatus(StatusEnum.VALID.getIndex());
            role.setEnableStatus(EnableStatusEnum.ENABLE.getIndex());
            roleService.addRole(role);
        }
        return ResponseResult.ok();
    }
   /**
    * @Title: 查看角色信息
    * @Description:roleRequestVo @{Link RoleRequestVo}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 16:58:08
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping(value = "detail")
    public String detail(ModelMap modelMap, String id){
        Role role = null;
        if(StringUtils.isBlank(id)){
            role = roleService.getRole(id);
        }
        modelMap.put("role", role);
        return "sec/role_detail";
    }
    /**
     * @Title: 跳转角色编辑界面
     * @Description:id @{Link Long}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 16:58:08
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @RequestMapping(value = "edit")
    public String edit(ModelMap modelMap, @RequestParam String id){
        Role role = null;
        if(StringUtils.isBlank(id)){
            role = roleService.getRole(id);
        }
        modelMap.put("role", role);
        return "sec/role_edit";
    }
    /**
     * @Title: 删除角色信息
     * @Description:id @{Link Long}
     * @param
     * @return
     * @throw
     * @author lixiangling
     * @date 2018/08/13 16:58:08
     *  --------------------------------------------------------
     *  modifyer    modifyTime                 comment
     *
     *  --------------------------------------------------------
     */
    @ResponseBody
    @RequestMapping("del")
    public ResponseResult del( @RequestParam(value = "id") String[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        roleService.del(ids);
        return ResponseResult.ok();
    }
   /**
    * @Title: 启用角色信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 16:58:08
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "turnOn")
    public ResponseResult turnOn(@RequestParam(value = "id") String[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        roleService.turnOn(ids);
        return ResponseResult.ok();
    }
   /**
    * @Title: 禁用角色信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 16:58:08
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @ResponseBody
    @RequestMapping(value = "turnOff")
    public ResponseResult turnOff(@RequestParam(value = "id") String[] ids){
        if (ids == null || ids.length == 0) {
            return ResponseResult.error(SysMsgEnumType.SYS_WEB_CHECK_INFO_FAIL);
        }
        roleService.turnOff(ids);
        return ResponseResult.ok();
    }

    /**
    * @Title: 导出角色信息
    * @Description:id @{Link Long}
    * @param
    * @return
    * @throw
    * @author lixiangling
    * @date 2018/08/13 16:58:08
    *  --------------------------------------------------------
    *  modifyer    modifyTime                 comment
    *
    *  --------------------------------------------------------
    */
    @RequestMapping("export")
    public void export(HttpServletResponse response, RoleRequestVo roleRequestVo){
        Role role = new Role();
        BeanUtil.copyProperties(roleRequestVo, role);
        PageData<RoleResponseVo> rolePage = roleService.getRolePage(role, roleRequestVo.getPage(), roleRequestVo.getLimit());
        List<RoleResponseVo> data = rolePage.getData();

        ColumnBox columnBox = ColumnBox.create()
                
			.add("roleCode", "角色编码")
			.add("roleName", "角色名称")
			.add("status", "状态")
			.add("reorder", "排序")
			.add("remark", "备注")
			.add("enableStatus", "启用状态 启用-1、禁用-0");

        try {
            ExportUtils.write(String.format("角色_%s.xls",DateUtil.date("yyyy-MM-dd",new Date().getTime())), response, data, columnBox);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}